﻿using System;
using System.Data;
using System.Collections.Generic;
using Common;
using Model;
namespace BLL
{
	/// <summary>
	/// WenZhang
	/// </summary>
	public partial class WenZhang
	{
		private readonly DAL.WenZhang dal=new DAL.WenZhang();
		public WenZhang()
		{}
		#region  Method

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
			return dal.GetMaxId();
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int ID)
		{
			return dal.Exists(ID);
		}

		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int  Add(Model.WenZhang model)
		{
			return dal.Add(model);
		}

		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(Model.WenZhang model)
		{
			return dal.Update(model);
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int ID)
		{
			
			return dal.Delete(ID);
		}
		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool DeleteList(string IDlist )
		{
			return dal.DeleteList(IDlist );
		}

		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Model.WenZhang GetModel(int ID)
		{
			
			return dal.GetModel(ID);
		}

		/// <summary>
		/// 得到一个对象实体，从缓存中
		/// </summary>
		public Model.WenZhang GetModelByCache(int ID)
		{
			
			string CacheKey = "WenZhangModel-" + ID;
			object objModel = Common.DataCache.GetCache(CacheKey);
			if (objModel == null)
			{
				try
				{
					objModel = dal.GetModel(ID);
					if (objModel != null)
					{
						int ModelCache = Common.ConfigHelper.GetConfigInt("ModelCache");
						Common.DataCache.SetCache(CacheKey, objModel, DateTime.Now.AddMinutes(ModelCache), TimeSpan.Zero);
					}
				}
				catch{}
			}
			return (Model.WenZhang)objModel;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			return dal.GetList(strWhere);
		}
		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			return dal.GetList(Top,strWhere,filedOrder);
		}
		/// <summary>
		/// 获得数据列表
		/// </summary>
		public List<Model.WenZhang> GetModelList(string strWhere)
		{
			DataSet ds = dal.GetList(strWhere);
			return DataTableToList(ds.Tables[0]);
		}
		/// <summary>
		/// 获得数据列表
		/// </summary>
		public List<Model.WenZhang> DataTableToList(DataTable dt)
		{
			List<Model.WenZhang> modelList = new List<Model.WenZhang>();
			int rowsCount = dt.Rows.Count;
			if (rowsCount > 0)
			{
				Model.WenZhang model;
				for (int n = 0; n < rowsCount; n++)
				{
					model = new Model.WenZhang();
					if(dt.Rows[n]["ID"]!=null && dt.Rows[n]["ID"].ToString()!="")
					{
						model.ID=int.Parse(dt.Rows[n]["ID"].ToString());
					}
					if(dt.Rows[n]["Title"]!=null && dt.Rows[n]["Title"].ToString()!="")
					{
					model.Title=dt.Rows[n]["Title"].ToString();
					}
					if(dt.Rows[n]["Contents"]!=null && dt.Rows[n]["Contents"].ToString()!="")
					{
					model.Contents=dt.Rows[n]["Contents"].ToString();
					}
					if(dt.Rows[n]["IsTop"]!=null && dt.Rows[n]["IsTop"].ToString()!="")
					{
						model.IsTop=int.Parse(dt.Rows[n]["IsTop"].ToString());
					}
					if(dt.Rows[n]["UserID"]!=null && dt.Rows[n]["UserID"].ToString()!="")
					{
						model.UserID=int.Parse(dt.Rows[n]["UserID"].ToString());
					}
					if(dt.Rows[n]["Sort"]!=null && dt.Rows[n]["Sort"].ToString()!="")
					{
						model.Sort=int.Parse(dt.Rows[n]["Sort"].ToString());
					}
					if(dt.Rows[n]["DiJiCount"]!=null && dt.Rows[n]["DiJiCount"].ToString()!="")
					{
						model.DiJiCount=int.Parse(dt.Rows[n]["DiJiCount"].ToString());
					}
					if(dt.Rows[n]["DateTime"]!=null && dt.Rows[n]["DateTime"].ToString()!="")
					{
						model.DateTime=DateTime.Parse(dt.Rows[n]["DateTime"].ToString());
					}
					if(dt.Rows[n]["Rmark"]!=null && dt.Rows[n]["Rmark"].ToString()!="")
					{
					model.Rmark=dt.Rows[n]["Rmark"].ToString();
					}
					modelList.Add(model);
				}
			}
			return modelList;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetAllList()
		{
			return GetList("");
		}

		/// <summary>
		/// 分页获取数据列表
		/// </summary>
        public DataSet GetList(int PageSize, int PageIndex, string strWhere)
        {
            return dal.GetList(PageSize, PageIndex, strWhere);
        }


        public DataTable  GetArticlTYpe()
        {
            DataSet ds = dal.GetArticlTYpe();
            DataTable dt=null;
            if(ds!=null && ds.Tables.Count>0)
            {
               dt = ds.Tables[0];
            }
            return dt;
        }
		#endregion  Method
	}
}

